<template>
  <div class="shopcar-container">
    <div class="goods-list">
      <div class="mui-card" v-for="(item, i) in shopCarList" :key="item.id">
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<mt-switch @change="swithcChange(item.id, $store.getters.getGoodsSelect[item.id])" v-model="$store.getters.getGoodsSelect[item.id]"></mt-switch>
            <img :src="item.img" alt="">
            <div class="info">
              <h1>{{ item.title }}</h1>
              <div class="price">
                <span>￥{{ item.price }}</span>
                <numbox :count="$store.getters.getIdCountList[item.id]" :id="item.id"></numbox>
                <a href="#" @click.prevent="removeShopCar(item.id, i)">删除</a>
              </div>
            </div>
					</div>
				</div>
			</div>

      <div class="mui-card">
				<div class="mui-card-content">
					<div class="mui-card-content-inner count">
						<div class="left">
              <p>总计（不含运费）</p>
              <p>已勾选商品总数<span class="red">{{ this.$store.getters.getShopCarCountAndPrice.count }}</span>件，
              总价￥<span class="red">{{ this.$store.getters.getShopCarCountAndPrice.price }}</span></p>
            </div>
            <mt-button type="danger">去结算</mt-button>
					</div>
				</div>
			</div>
    </div>
  </div>
</template>

<script>
import numbox from '../subcomponents/shopcar_numberbox.vue'
import { Toast } from 'mint-ui'

export default {
  data(){
    return{
      shopCarList: []
    }
  },
  methods: {
    getShopCars() {
      this.$store.state.shopCar.forEach(element => {
        this.$http.get('shopCars/'+element.id).then(result => {
          this.shopCarList.push(result.body)
        }, error => {
          Toast('购物车内容获取失败！')
        })
      })
    },
    removeShopCar(id, index){
      // 删除store中的数据
      this.$store.commit('deleteShopCarGoodsById', id)
      // 删除当前shopCarList中的数据
      this.shopCarList.splice(index, 1)
    },
    swithcChange(id, val) {
      this.$store.commit('updateShopCarSelectState', {id: id, status: val})
    }
  },
  created() {
    this.getShopCars()
  },
  components: {
    'numbox': numbox
  }
}
</script>

<style lang="scss" scoped>
  .shopcar-container{
    background-color: #eee;
    overflow: hidden;
    .mui-card-content-inner{
      display: flex;
    }
    .count{
      display: flex;
      justify-content: space-between;
      align-items: center;
      .red{
        color: red;
        font-weight: bold;
        font-size: 14px;
      }
    }
    .goods-list{
      img{
        width: 60px;
        height: 60px;
      }
      .info{
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        h1{
          font-size: 13px;
          font-weight: bold;
          margin-bottom: 10px;
        }
      }
    }
  }
</style>


